package com.heima.canal.freemarker;

import com.heima.article.api.ArticleContentFeign;
import com.heima.article.api.DfsControllerFeign;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


/**
 * @author mgw
 * @create 2021/9/27 0:14
 */
@Component
public class ArticlePageHandler {
    @Autowired
    private Configuration configuration;
    @Autowired
    private ArticleContentFeign articleContentFeign;
    @Autowired
    private DfsControllerFeign dfsControllerFeign;
    public void article(ApArticle apArticle) throws IOException, TemplateException {
        Template template = configuration.getTemplate("article.ftl");
        Map<String,Object> dateMap=new HashMap<String,Object>();
        dateMap.put("article",apArticle);
        ResponseResult<ApArticleContent> article = articleContentFeign.getArticle(apArticle.getId());
   //     dateMap.put("articleContent",article.getData());
  //      template.process(dateMap,new PrintWriter("D:/1.html"));
        String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, dateMap);
        System.out.println(content);
        ResponseResult responseResult = dfsControllerFeign.uploadHtml(content);
        String url = responseResult.getData().toString();
   //     System.out.println("输出的地址是=============="+url);
        articleContentFeign.updateStaticUrl(url,apArticle.getId());
    }
}
